package com.nannybear.View;

import android.os.Handler;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nannybear.R;

/**
 * \brief The remote screen
 * \author Audrey LOISEAU
 * \date 16-03-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class RemoteScreen extends Screen{
    /********** DECLARATIONS **********/
    // Widget of the view
    private ImageButton animation_btn, default_btn, led_btn, time_btn, start_lullaby;
    private boolean animationIsOpen, defaultIsOpen, ledIsOpen;
    private ImageView led_middle, led_part1_1, led_part1_2, led_part2_1, led_part2_2, led_part3_1, led_part3_2, led_part4_1, led_part4_2;
    private int colorLedPart1, colorLedPart2, colorLedMiddle, colorLedPart3, colorLedPart4;
    private RelativeLayout layout_led;
    private RelativeLayout animation_layout;
    private Button start, stop, animation_1, animation_2;
    private TextView time;
    private int colorLed;
    private WebView gifView;

    // Handler
    private final Handler myHandler = new Handler();
    
    /************ METHODS ************/
    /**
     * \brief Constructor
     */
    public RemoteScreen(){
        super(R.layout.remote_screen, false, false);
        animationIsOpen = false;
        defaultIsOpen = false;
        ledIsOpen = false;
    }

    /**
     * \brief Change the state of all buttons for the night light
     * \param btn   The button clicked
     */
    public void changeNightLight(ImageButton btn){
        if(btn == animation_btn){
            if(animationIsOpen){
                animation_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                animation_layout.setVisibility(View.GONE);
                animation_2.setBackgroundResource(R.drawable.button_grey_drawable);
                animation_1.setBackgroundResource(R.drawable.button_grey_drawable);
                animationIsOpen = false;
            }
            else{
                if(ledIsOpen){
                    led_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                    layout_led.setVisibility(View.GONE);
                    ledIsOpen = false;
                }
                if(defaultIsOpen){
                    default_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                    default_btn.setImageResource(R.drawable.green_led_drawable);
                    defaultIsOpen = false;
                    colorLed = 0;
                }
                animation_btn.setBackgroundResource(R.drawable.button_drawable);
                animationIsOpen = true;
                animation_layout.setVisibility(View.VISIBLE);
            }
        }
        else if(btn == led_btn){
            if(ledIsOpen){
                led_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                layout_led.setVisibility(View.GONE);
                ledIsOpen = false;
            }
            else{
                if(animationIsOpen){
                    animation_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                    animation_layout.setVisibility(View.GONE);
                    animation_2.setBackgroundResource(R.drawable.button_grey_drawable);
                    animation_1.setBackgroundResource(R.drawable.button_grey_drawable);
                    animationIsOpen = false;
                }
                if(defaultIsOpen){
                    default_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                    default_btn.setImageResource(R.drawable.green_led_drawable);
                    defaultIsOpen = false;
                    colorLed = 0;
                }
                layout_led.setVisibility(View.VISIBLE);
                led_btn.setBackgroundResource(R.drawable.button_drawable);
                ledIsOpen = true;
            }
        }
        else if(btn == default_btn) {
            // Led is red
            if (defaultIsOpen && colorLed == 1) {
                default_btn.setImageResource(R.drawable.red_led_drawable);
                colorLed = 2;
            }
            // Led is orange
            else if (defaultIsOpen && colorLed == 2) {
                default_btn.setImageResource(R.drawable.orange_led_drawable);
                colorLed = 3;
            }
            // Put the button to "off" mode, led is green
            else if (defaultIsOpen && colorLed == 3) {
                default_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                default_btn.setImageResource(R.drawable.green_led_drawable);
                colorLed = 0;
                defaultIsOpen = false;
            }
            else{
                if(animationIsOpen){
                    animation_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                    animation_layout.setVisibility(View.GONE);
                    animation_2.setBackgroundResource(R.drawable.button_grey_drawable);
                    animation_1.setBackgroundResource(R.drawable.button_grey_drawable);
                    animationIsOpen = false;
                }
                if(ledIsOpen){
                    led_btn.setBackgroundResource(R.drawable.button_grey_drawable);
                    layout_led.setVisibility(View.GONE);
                    ledIsOpen = false;
                }
                //Put the button in "on" mode, led is green
                default_btn.setBackgroundResource(R.drawable.button_drawable);
                defaultIsOpen = true;
                colorLed = 1;
            }
        }
        checkOpeningButton();
    }

    /**
     * \brief Check which buttons is opened to change the visibility
     */
    private void checkOpeningButton(){
        if(!animationIsOpen && !ledIsOpen && !defaultIsOpen){
            start.setVisibility(View.GONE);
            stop.setVisibility(View.GONE);
        }
        else{
            start.setVisibility(View.VISIBLE);
            stop.setVisibility(View.VISIBLE);
        }
    }

    /**
     * \brief Change the color of the led board
     * \param partLed   The led package
     */
    public void changeLedBoard(int partLed){
        if(partLed == 1){
            // If it was black, change to green
            if(colorLedPart1 == 0) {
                colorLedPart1 = 1;  // green
                led_part1_1.setBackgroundResource(R.drawable.green_led_drawable);
                led_part1_2.setBackgroundResource(R.drawable.green_led_drawable);
            }
            // if it was green, change to red
            else if(colorLedPart1 == 1){
                colorLedPart1 = 2;  // red
                led_part1_1.setBackgroundResource(R.drawable.red_led_drawable);
                led_part1_2.setBackgroundResource(R.drawable.red_led_drawable);
            }
            // If it was red, change to orange
            else if(colorLedPart1 == 2){
                colorLedPart1 = 3;  // black
                led_part1_1.setBackgroundResource(R.drawable.orange_led_drawable);
                led_part1_2.setBackgroundResource(R.drawable.orange_led_drawable);
            }
            // If it was orange, change to black
            else{
                colorLedPart1 = 0;  // black
                led_part1_1.setBackgroundResource(R.drawable.off_led_drawable);
                led_part1_2.setBackgroundResource(R.drawable.off_led_drawable);
            }
        }
        else if(partLed == 2) {
            // If it was black, change to green
            if (colorLedPart2 == 0) {
                colorLedPart2 = 1;  // green
                led_part2_1.setBackgroundResource(R.drawable.green_led_drawable);
                led_part2_2.setBackgroundResource(R.drawable.green_led_drawable);
            }
            // if it was green, change to red
            else if (colorLedPart2 == 1) {
                colorLedPart2 = 2;  // red
                led_part2_1.setBackgroundResource(R.drawable.red_led_drawable);
                led_part2_2.setBackgroundResource(R.drawable.red_led_drawable);
            }
            // if it was red, change to orange
            else if (colorLedPart2 == 2) {
                colorLedPart2 = 3;  // red
                led_part2_1.setBackgroundResource(R.drawable.orange_led_drawable);
                led_part2_2.setBackgroundResource(R.drawable.orange_led_drawable);
            }
            // If it was orange, change to black
            else {
                colorLedPart2 = 0;  // black
                led_part2_1.setBackgroundResource(R.drawable.off_led_drawable);
                led_part2_2.setBackgroundResource(R.drawable.off_led_drawable);
            }
        }
        else if(partLed == 3) {
            // If it was black, change to green
            if (colorLedPart3 == 0) {
                colorLedPart3 = 1;  // green
                led_part3_1.setBackgroundResource(R.drawable.green_led_drawable);
                led_part3_2.setBackgroundResource(R.drawable.green_led_drawable);
            }
            // if it was green, change to red
            else if (colorLedPart3 == 1) {
                colorLedPart3 = 2;  // red
                led_part3_1.setBackgroundResource(R.drawable.red_led_drawable);
                led_part3_2.setBackgroundResource(R.drawable.red_led_drawable);
            }
            // if it was red, change to orange
            else if (colorLedPart3 == 2) {
                colorLedPart3 = 3;  // red
                led_part3_1.setBackgroundResource(R.drawable.orange_led_drawable);
                led_part3_2.setBackgroundResource(R.drawable.orange_led_drawable);
            }
            // If it was orange, change to black
            else {
                colorLedPart3 = 0;  // black
                led_part3_1.setBackgroundResource(R.drawable.off_led_drawable);
                led_part3_2.setBackgroundResource(R.drawable.off_led_drawable);
            }
        }
        else if(partLed == 4) {
            // If it was black, change to green
            if (colorLedPart4 == 0) {
                colorLedPart4 = 1;  // green
                led_part4_1.setBackgroundResource(R.drawable.green_led_drawable);
                led_part4_2.setBackgroundResource(R.drawable.green_led_drawable);
            }
            // if it was green, change to red
            else if (colorLedPart4 == 1) {
                colorLedPart4 = 2;  // red
                led_part4_1.setBackgroundResource(R.drawable.red_led_drawable);
                led_part4_2.setBackgroundResource(R.drawable.red_led_drawable);
            }
            // if it was red, change to orange
            else if (colorLedPart4 == 2) {
                colorLedPart4 = 3;  // orange
                led_part4_1.setBackgroundResource(R.drawable.orange_led_drawable);
                led_part4_2.setBackgroundResource(R.drawable.orange_led_drawable);
            }
            // If it was orange, change to black
            else {
                colorLedPart4 = 0;  // black
                led_part4_1.setBackgroundResource(R.drawable.off_led_drawable);
                led_part4_2.setBackgroundResource(R.drawable.off_led_drawable);
            }
        }
        else if(partLed == 5){
            // If it was black, change to green
            if (colorLedMiddle == 0) {
                colorLedMiddle = 1;  // green
                led_middle.setBackgroundResource(R.drawable.green_led_drawable);
            }
            // if it was green, change to red
            else if (colorLedMiddle == 1) {
                colorLedMiddle = 2;  // red
                led_middle.setBackgroundResource(R.drawable.red_led_drawable);
            }
            // if it was red, change to orange
            else if (colorLedMiddle == 2) {
                colorLedMiddle = 3;  // orange
                led_middle.setBackgroundResource(R.drawable.orange_led_drawable);
            }
            // If it was orange, change to black
            else {
                colorLedMiddle = 0;  // black
                led_middle.setBackgroundResource(R.drawable.off_led_drawable);
            }
        }
    }

    /**
     * \brief Load the gif animation
     * \param anim  Wich animation is wanted
     */
    public void loadGif(int anim){
        if(this.gifView.getVisibility() != View.VISIBLE){
            this.gifView.setVisibility(View.VISIBLE);
        }
        if(anim == 1){
            this.gifView.loadUrl("file:///android_asset/gif/anim1.gif");
        }
        else{
            this.gifView.loadUrl("file:///android_asset/gif/anim2.gif");
        }
    }

    /**
     * \brief Show the view
     * \param thresholdTemp The value of temperature threshold
     * \param thresholdHum  The value of humidity threshold
     */
    public void display(int thresholdTemp, int thresholdHum){
        colorLed = 0;   //button in grey
        // Color of led = black
        colorLedMiddle = 0;
        colorLedPart1 = 0;
        colorLedPart2 = 0;
        colorLedPart3 = 0;
        colorLedPart4 = 0;
        getTempButton().setBackgroundResource(getTempDrawable()[thresholdTemp]);
        getHumidityButton().setBackgroundResource(getHumDrawable()[thresholdHum]);
        getRemoteButton().setBackgroundResource(R.drawable.remote_active);
    }

    /**
     * \brief Close the view (empty)
     */
    public void close() {
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Get method to know if the animation layout is open
     */
    public boolean isAnimationIsOpen() {
        return animationIsOpen;
    }

    /**
     * \brief Get method to know if the default layout is open
     */
    public boolean isDefaultIsOpen() {
        return defaultIsOpen;
    }

    /**
     * \brief Get method to know if the led layout is open
     */
    public boolean isLedIsOpen() {
        return ledIsOpen;
    }

    /**
     * \brief Get method for the time
     */
    public TextView getTime() {
        return time;
    }

    /**
     * \brief Get method for the led color
     */
    public int getColorLed() {
        return colorLed;
    }

    /**
     * \brief Get method for the led color (part 1)
     */
    public int getColorLedPart1() {
        return colorLedPart1;
    }

    /**
     * \brief Get method for the led color (part 2)
     */
    public int getColorLedPart2() {
        return colorLedPart2;
    }

    /**
     * \brief Get method for the middle led color
     */
    public int getColorLedMiddle() {
        return colorLedMiddle;
    }

    /**
     * \brief Get method for the led color (part 3)
     */
    public int getColorLedPart3() {
        return colorLedPart3;
    }

    /**
     * \brief Get method for the led color (part 4)
     */
    public int getColorLedPart4() {
        return colorLedPart4;
    }

    /**
     * \brief Get method for he gif view
     */
    public WebView getGifView() {
        return gifView;
    }

    /**
     * \brief Set method for the animation button
     */
    public void setAnimation_btn(ImageButton animation_btn) {
        this.animation_btn = animation_btn;
    }

    /**
     * \brief Set method for the default button
     */
    public void setDefault_btn(ImageButton start_btn) {
        this.default_btn = start_btn;
    }

    /**
     * \brief Set method for the led button
     */
    public void setLed_btn(ImageButton led_btn) {
        this.led_btn = led_btn;
    }

    /**
     * \brief Set method for the middle led button
     */
    public void setLed_middle(ImageView led_middle) {
        this.led_middle = led_middle;
    }

    /**
     * \brief Set method for the led 1 part 1
     */
    public void setLed_part1_1(ImageView led_part1_1) {
        this.led_part1_1 = led_part1_1;
    }

    /**
     * \brief Set method for the led 2 part 1
     */
    public void setLed_part1_2(ImageView led_part1_2) {
        this.led_part1_2 = led_part1_2;
    }

    /**
     * \brief Set method for the led 1 part 2
     */
    public void setLed_part2_1(ImageView led_part2_1) {
        this.led_part2_1 = led_part2_1;
    }

    /**
     * \brief Set method for the led 2 part 2
     */
    public void setLed_part2_2(ImageView led_part2_2) {
        this.led_part2_2 = led_part2_2;
    }

    /**
     * \brief Set method for the led layout
     */
    public void setLayout_led(RelativeLayout layout_led) {
        this.layout_led = layout_led;
    }

    /**
     * \brief Set method for the led 1 part 3
     */
    public void setLed_part3_1(ImageView led_part3_1) {
        this.led_part3_1 = led_part3_1;
    }

    /**
     * \brief Set method for the led 2 part 3
     */
    public void setLed_part3_2(ImageView led_part3_2) {
        this.led_part3_2 = led_part3_2;
    }

    /**
     * \brief Set method for the led 1 part 4
     */
    public void setLed_part4_1(ImageView led_part4_1) {
        this.led_part4_1 = led_part4_1;
    }

    /**
     * \brief Set method for the led 2 part 4
     */
    public void setLed_part4_2(ImageView led_part4_2) {
        this.led_part4_2 = led_part4_2;
    }

    /**
     * \brief Set method for the start button
     */
    public void setStart(Button start) {
        this.start = start;
    }

    /**
     * \brief Set method for the time
     */
    public void setTime(TextView time) {
        this.time = time;
    }

    /**
     * \brief Set method for the time button
     */
    public void setTime_btn(ImageButton time_btn) {
        this.time_btn = time_btn;
    }

    /**
     * \brief Set method for the animation layout
     */
    public void setAnimation_layout(RelativeLayout animation_layout) {
        this.animation_layout = animation_layout;
    }

    /**
     * \brief Set method for the stop button
     */
    public void setStop(Button stop) {
        this.stop = stop;
    }

    /**
     * \brief Set method for the animation 1 button
     */
    public void setAnimation_1(Button animation_1) {
        this.animation_1 = animation_1;
    }

    /**
     * \brief Set method for the animation 2 button
     */
    public void setAnimation_2(Button animation_2) {
        this.animation_2 = animation_2;
    }

    /**
     * \brief Set method for the gif
     */
    public void setGifView(WebView gifView) {
        this.gifView = gifView;
    }

    /**
     * \brief Set method for the lullaby start button
     */
    public void setStart_lullaby(ImageButton start_lullaby) {
        this.start_lullaby = start_lullaby;
    }
}
